home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / self / contrib.lha / contrib / simulator / sim.self < prev   
Encoding:
Text File  |  1991-05-06  |  16.8 KB  |  529 lines

  1. "$ Revision: 1.1 $"
  2. "Copyright 1990:
  3.     Jecel Mattos de Assumpcao Jr.
  4.     LSI - EPSUP, Sao Paulo, SP, Brasil"
  5.  
  6.     "Generic Simulation Objects "
  7.  
  8.     "traits simulation component"
  9.     "<prototypes simulation> component"
  10.     "Parts that actually do the work during the simulation"
  11.  
  12.     "<prototypes simulation> library"
  13.     "Name space for standard components"
  14.  
  15.     "traits simulation net"
  16.     "<prototypes simulation> net"
  17.     "Connects Pins to each other"
  18.  
  19.     "traits simulation pin"
  20.     "<prototypes simulation> pin"
  21.     "Connects a Component to a Net"
  22.  
  23.     "<prototypes simulation> event"
  24.     "Schedules a change of value at a Pin"
  25.  
  26.     "<prototypes simulation> wave"
  27.     "Holds the waveform for a Pin"
  28.  
  29.     "<prototypes simulation> fsm"
  30.     "Implements Finite State Machines"
  31.  
  32.     "traits simulation runTime"
  33.     "<prototypes simulation> runTime"
  34.     "Run Time simulation support"
  35.  
  36. traits simulation _DefineSlots: ( |
  37.     component = ( |
  38.         cloning** = traits clonable.
  39.         comparing** = mixins comparisons identity.
  40.         fsm = ( prototypes simulation fsm copy ).
  41.         part = ( self ).
  42.         name = ( dad == nil ifTrue: [ ^ '' ]
  43.                  False: [ | dm. |
  44.                      dm: reflect: dad.
  45.                      dm size do: [ | :i |
  46.                          ( dm contentsAt: i ) isReflecteeMethod
  47.                          ifFalse: [
  48.                              == ( dm contentsAt: i ) reflectee ifTrue: [ 
  49.                                  ^ dad name , ' ' , ( dm nameAt: i ) ].
  50.                              ( dm contentsAt: i ) isReflecteeVector ifTrue: [
  51.                                  | v |
  52.                                  v: ( dm contentsAt: i ) reflectee.
  53.                                  v size do: [ | :j |
  54.                                      == ( v at: j ) ifTrue: [
  55.                                          ^ '(' , dad name , ' ' , ( dm nameAt:
  56.                                              i ) , ' at: ' , j printString ,
  57.                                              ' )' ].
  58.                                  ].
  59.                              ].
  60.                          ].
  61.                      ].
  62.                  error: 'bad component hierarchy'.
  63.                  ].
  64.         ).
  65.         expandable = ( | m |
  66.             m: reflect: self.
  67.             m size do: [ | :i |
  68.                 ( ( m nameAt: i ) = 'netlist' ) ifTrue: [ ^ true ].
  69.             ].
  70.             false.
  71.         ).
  72.         executable = ( | m |
  73.             m: reflect: self.
  74.             m size do: [ | :i |
  75.                 ( ( m nameAt: i ) = 'behave' ) ifTrue: [ ^ true ].
  76.             ].
  77.             false.
  78.         ).
  79.         set: pin To: value After: time = ( | e. |
  80.             e: event copy.
  81.             e pin: pin.
  82.             e value: value.
  83.             schedule: e After: time.
  84.         ).
  85.         set: pin To: value = (
  86.             set: pin To: value After: 0.
  87.         ).
  88.         simulate = (
  89.             runTime copy getRoot: self.
  90.             'detailed simulation - type help for more information'
  91.         ).
  92.         exert = ( | s |
  93.             s: runTime copy. 
  94.             s expandAll: false.
  95.             s getRoot: self. 
  96.             'high level simulation - type help for more information'
  97.         ).
  98.         askSim = ( | s |
  99.             s: runTime copy. 
  100.             s expandAll: false.
  101.             s askExpand: true.
  102.             s getRoot: self. 
  103.             'mixed level simulation - type help for more information'
  104.         ).
  105.         layout = ( 'NOT IMPLEMENTED YET !' ).
  106.     | ).
  107.     net = ( |
  108.         copy = ( | n. |
  109.             n: clone.
  110.             n pins: pins copy.
  111.             n.
  112.         ).
  113.         restart = (  | v. |
  114.             v:  pins oddballs simulation levels highImpedance.
  115.             pins do: [ | :p. |
  116.                 v: p value resolve: v
  117.         ].
  118.            value: v
  119.         ).
  120.         reCheckFor: aSim = ( | v. |
  121.             v: aSim z.
  122.             pins do: [ | :p. |
  123.                 v: p value resolve: v
  124.         ].
  125.         value == v ifFalse: [
  126.         value: v.
  127.         pins do: [ | :p. |
  128.             ( p input ) && ( p component leaf ) 
  129.             ifTrue: [ aSim notify add: p component ].
  130.                     p wave == nil ifFalse: [ p wave become: v ].
  131.         ].
  132.         ].
  133.     ).
  134.     | ).
  135.     simPin = ( |
  136.         myName = ( | cm. |
  137.                      cm: reflect: component.
  138.                      cm size do: [ | :i |
  139.                          ( cm contentsAt: i ) isReflecteeMethod
  140.                           ifFalse: [
  141.                               == ( cm contentsAt: i ) reflectee
  142.                                  ifTrue: [ ^ cm nameAt: i ].
  143.                          ].
  144.                      ].
  145.                  error: 'bad component'.
  146.         ).
  147.         printString = ( component name ,
  148.                         ' ' , myName ,
  149.                         ' --> ' ,
  150.                         net value printString ).
  151.         to: aPin = (
  152.             (net == aPin net)
  153.                 ifFalse: [ net pins addAll: aPin net pins.
  154.                            aPin net pins do: [ | :p |
  155.                                            p net: net
  156.                            ].
  157.                 ].
  158.         ).
  159.         level = ( net value level ).
  160.         not = ( net value not ).
  161.         || aPin = ( ( net value ) || ( aPin net value ) ).
  162.         && aPin = ( ( net value ) && ( aPin net value ) ).
  163.     | ).
  164.     runTime = ( |
  165.         parent** = traits clonable.
  166.         userCommands* = ( |
  167.             restart = (
  168.                 now: 0.
  169.                 events: treeBag copy.
  170.                 pins do: [ | :p |
  171.                     ( p component leaf ) 
  172.                         ifTrue: [ 
  173.                          p restart.
  174.                          p net restart. 
  175.                     ].
  176.                 ].
  177.             ).
  178.             step = (
  179.                 events isEmpty ifFalse: [
  180.                     notify: set copy.
  181.                     checkNets: set copy.
  182.                     now: events leftMost key.
  183.                     [ events isEmpty
  184.                        ifFalse: [ events leftMost key == now ]
  185.                        True: [ false ] ]
  186.                     whileTrue: [ | e. |
  187.                         e: events first.
  188.                         events remove: now.
  189.                         e pin value: e value.
  190.                         checkNets add: e pin net.
  191.                     ].
  192.                     checkNets do: [ | :n | n reCheckFor: self ].
  193.                     notify do: [ | :c | c behave ].
  194.                 ].
  195.                 now.
  196.             ).
  197.             stepBy: interval = (
  198.                 stepTo: now + interval.
  199.                 now.
  200.             ).
  201.             stepTo: time = (
  202.                 [ events isEmpty
  203.                     ifFalse: [ events leftMost key < time ]
  204.                      True: [ false ] ]
  205.                 whileTrue: [ step. ].
  206.                 now: time.
  207.                 now.
  208.             ).
  209.             run = ( [ events isEmpty ] whileFalse: [ step ]. now. ).
  210.             watch: aPin = ( 
  211.                 wList add: aPin.
  212.                 aPin wave: prototypes simulation wave copy.
  213.                 aPin wave sim: sim.
  214.                 aPin wave become: aPin net value.
  215.                 aPin.
  216.             ).
  217.             watch = ( wList do: [ | :p | p printLine ]. 'watch pins' ).
  218.             all = ( pins do: [ | :p | p printLine ]. 'all pins' ).
  219.             draw = ( | g. sx. sy. k. |
  220.                 g: heightLimitedScreenBitmap copy.
  221.                 g initialize.
  222.                 g fillRectangle: 0 @ 0 To: 1000 @ 600 Color: colors white.
  223.                 g changeFont: 'screen.b.14'.
  224.                 sx: 200.
  225.                 sy: 20.
  226.                 k: 600 / wList size.
  227.                 wList do: [ | :p. t. |
  228.                     t: paragraph copyLines: p component name ,
  229.                                                 ' ' , p myName.
  230.                     g text: t At: 10 @ sy.
  231.                     p wave drawOn: g At: sx @ sy.
  232.                     sy: sy + k.
  233.                 ].
  234.                 0 to: now By: 10 Do: [ | :time. x. |
  235.                     x: ( ( ( time - offset ) * scale ) + sx ).
  236.                     ( x < 980 ) && ( x >= sx ) ifTrue: [
  237.                         g text: ( paragraph copyLines: time printString )
  238.                             At: x @ 580.
  239.                     ].
  240.                 ].
  241.                 g release.
  242.             ).
  243.             help = ( '
  244.  
  245. HELP mixed level digital event-driven simulator.
  246.  
  247. user commands:
  248.  
  249.     restart          - sets the simulation to the initial state
  250.     step             - simulate until something changes
  251.     run              - simulate until everything is stable
  252.     stepTo: TIME     - simulate until the specified TIME in ns
  253.     stepBy: INTERVAL - simulate for specified INTERVAL ns
  254.     watch: PIN       - adds PIN to the watch list
  255.     watch            - show current values of pins in the watch list
  256.     all              - show current values of all pins
  257.     draw             - draw the waveforms of pins in the watch list
  258.     help             - prints this message
  259.             ' ).
  260.         | ).
  261.         operation* = ( |
  262.             schedule: anEvent After: interval = ( 
  263.                 schedule: anEvent At: now + interval.
  264.             ).
  265.             schedule: anEvent At: time = (
  266.                 events at: time Put: anEvent.
  267.             ).
  268.         | ).
  269.         values* = ( |
  270.             x = oddballs simulation levels unknown.
  271.             l = oddballs simulation levels drivenLow.
  272.             h = oddballs simulation levels drivenHigh.
  273.             z = oddballs simulation levels highImpedance.
  274.         | ).
  275.         startUp* = ( |
  276.             get: aPart = ( | c. |
  277.                 c: aPart copy.
  278.                 c sim: sim.
  279.                 c dad: part.
  280.                 c leaf: true.
  281.                 leaf: false.
  282.                 c declare.
  283.                 expandAll && c expandable ifTrue: [ c netlist. ].
  284.                 expandAll not && c executable not ifTrue: [ c netlist. ].
  285.                 askExpand && c expandable && c executable ifTrue: [
  286.                     | ask = '                     ' copy. |
  287.                     'Do you wish to expand ' print.
  288.                     c type print.
  289.                     ' ? ' print.
  290.                     0 _ReadInto: ask Count: 10.
  291.                     ( 'Yy' includes: ( ask at: 0 ) ) ifTrue: [ c netlist ].
  292.                 ].
  293.                 c.
  294.             ).
  295.             sim = ( self ).
  296.             part = ( nil ).
  297.             leaf.
  298.             getRoot: aPart = (
  299.                 simRoot: nil.
  300.                 pins: pins copy.
  301.                 wList: wList copy.
  302.                 notify: notify copy.
  303.                 events: events copy. 
  304.                 root: ( get: aPart ).
  305.                 simRoot: root.
  306.             ).
  307.             vdd = ( | p. |
  308.                 vddg == nil ifFalse: [ vddg ]
  309.                            True: [
  310.                     p: supply copy.
  311.                     p component: root.
  312.                     p net: prototypes net copy.
  313.                     p net pins add: p.
  314.                     vddg: p.
  315.                     p.
  316.                 ].
  317.             ).
  318.             gnd = ( | p. |
  319.                 gndg == nil ifFalse: [ gndg ]
  320.                            True: [
  321.                     p: ground copy.
  322.                     p component: root.
  323.                     p net: prototypes net copy.
  324.                     p net pins add: p.
  325.                     gndg: p.
  326.                     p.
  327.                 ].
  328.             ).
  329.             output = ( | p. |
  330.                 p: outPin copy.
  331.                 p component: self.
  332.                 p value: z.
  333.                 p net: prototypes net copy.
  334.                 p net pins add: p.
  335.                 pins add: p.
  336.                 p.
  337.             ).
  338.             input = ( | p. |
  339.                 p: inPin copy.
  340.                 p component: self.
  341.                 p value: z.
  342.                 p net: prototypes net copy.
  343.                 p net pins add: p.
  344.                 pins add: p.
  345.                 p.
  346.             ).
  347.         | ).
  348.     | ).
  349.  | )
  350.  
  351. prototypes simulation _DefineSlots: ( |
  352.     component = ( |
  353.         parent* = traits simulation component.
  354.         sim**.
  355.         dad.
  356.         leaf.
  357.         type = 'prototypical circuit'.
  358.         declare = ().
  359.         behave = ().
  360.     | ).
  361.     library = ().
  362. | )
  363.  
  364. prototypes simulation _DefineSlots: ( |
  365.     net = ( |
  366.         parent* = traits simulation net.
  367.         cloning** = traits clonable.
  368.         comparing*** = mixins comparisons identity.
  369.         value.
  370.         pins <- set copy.
  371.     | ).
  372.     event = ( |
  373.         parent* = traits clonable.
  374.         thisObjectPrints = true.
  375.         printString = ( pin printString , '->' , value printString ).
  376.         value.
  377.         pin.
  378.     | ).
  379.     wave = ( |
  380.         parent* = traits clonable.
  381.         sim.
  382.         values.
  383.         times.
  384.         copy = ( | c. |
  385.              c: clone.
  386.              c values: list copy.
  387.              c times: list copy.
  388.              c.
  389.         ).
  390.         become: aValue = (
  391.             values add: aValue.
  392.             times add: sim now.
  393.         ).
  394.         drawOn: bitmap At: point = ( | x1. x2. signal. |
  395.             signal: sim z.
  396.             x2: point x - 30.
  397.             times with: values Do: [ | :t. :v. |
  398.                 x1: x2.
  399.                 x2: ( ( ( t - sim offset ) * sim scale ) + point x ).
  400.                 x2: ( ( 980 min: x2 ) max: point x ).
  401.                 signal drawOn: bitmap From: x1 @ point y 
  402.                                         To: x2 @ ( point y + 20 ).
  403.                 signal: v.
  404.             ].
  405.             signal drawOn: bitmap From: x2 @ point y
  406.                                     To: 980 @ ( point y + 20 ).
  407.         ).
  408.     | ).
  409.     fsm = ( |
  410.         parent** = traits clonable.
  411.         state <- ''.
  412.         next <- ''.
  413.         lastSignal.
  414.         initial: text = ( 
  415.             state: text.
  416.             next: text.
  417.             lastSignal: nil.
  418.         ).
  419.         onRising: pin Is: action = (
  420.             ( rising: pin net value ) ifTrue: [ 
  421.                 fsmRoot: self.
  422.                 action value. 
  423.                 fsmRoot: ().
  424.             ].
  425.         ).
  426.         onFalling: pin Is: action = (
  427.             ( falling: pin net value ) ifTrue: [
  428.                 fsmRoot: self.
  429.                 action value. 
  430.                 fsmRoot: ().
  431.             ].
  432.         ).
  433.         state: text Do: things = (
  434.             text = state ifTrue: [ things value ].
  435.         ).
  436.         goto: text = ( next: text ).
  437.         rising: signal = (
  438.             state: next.
  439.             signal isHigh
  440.                 ifTrue: [
  441.                     lastSignal isHigh ifTrue: [ ^ false ].
  442.                     lastSignal: signal.
  443.                     ^ true.
  444.             ].
  445.             lastSignal: signal.
  446.             false.
  447.         ).
  448.         falling: signal = (
  449.             state: next.
  450.             signal isLow
  451.                 ifTrue: [
  452.                     lastSignal isLow ifTrue: [ ^ false ].
  453.                     lastSignal: signal.
  454.                     ^ true.
  455.             ].
  456.             lastSignal: signal.
  457.             false.
  458.         ).
  459.     | ).
  460.     runTime = ( |
  461.         parent* = traits simulation runTime.
  462.         now <- 0.
  463.         expandAll <- true.
  464.         askExpand <- false.
  465.         events <- treeBag copy.
  466.         notify <- set copy.
  467.         checkNets <- set copy.
  468.         pins <- set copy.
  469.         wList <- list copy.
  470.         offset <- 0.
  471.         scale <- 5.
  472.         vddg.
  473.         gndg.
  474.         root <- prototypes simulation component.
  475.         supply = ( |
  476.             parent* = traits simulation simPin.
  477.             cloning** = traits clonable.
  478.             comparing** = mixins comparisons identity.
  479.             thisObjectPrints = true.
  480.             printString = 'vdd'.
  481.             component <- prototypes simulation component.
  482.             value = oddballs simulation levels drivenHigh.
  483.             net <- nil.
  484.             input = false.
  485.             wave.
  486.             restart = ().
  487.         | ).
  488.         ground = ( |
  489.             parent* = traits simulation simPin.
  490.             cloning** = traits clonable.
  491.             comparing** = mixins comparisons identity.
  492.             thisObjectPrints = true.
  493.             printString = 'gnd'.
  494.             component <- prototypes simulation component.
  495.             value = oddballs simulation levels drivenLow.
  496.             net <- nil.
  497.             input = false.
  498.             wave.
  499.             restart = ().
  500.         | ).
  501.         outPin = ( |
  502.             parent* = traits simulation simPin.
  503.             cloning** = traits clonable.
  504.             comparing** = mixins comparisons identity.
  505.             thisObjectPrints = true.
  506.             component <- prototypes simulation component.
  507.             value.
  508.             net <- nil.
  509.             input = false.
  510.             wave.
  511.             restart = ( value: oddballs simulation levels unknown ).
  512.         | ).
  513.         inPin = ( |
  514.             parent* = traits simulation simPin.
  515.             cloning** = traits clonable.
  516.             comparing** = mixins comparisons identity.
  517.             thisObjectPrints = true.
  518.             component <- prototypes simulation component.
  519.             value.
  520.             net <- nil.
  521.             input = true.
  522.             wave.
  523.             restart = ( value: oddballs simulation levels highImpedance ).
  524.         | ).
  525.     | ).
  526.     simRoot** <- ().
  527.     fsmRoot*** <- ().
  528. | )
  529.